﻿Imports System

Namespace System.Workflow.Runtime
    Friend Class ScheduleWork
        Implements IDisposable
        ' Methods
        Public Sub New(ByVal executor As WorkflowExecutor)
            Me.oldValue = ScheduleWork.scheduleInfo
            ScheduleWork.scheduleInfo = New ScheduleInfo(executor, False)
        End Sub

        Public Sub New(ByVal executor As WorkflowExecutor, ByVal suppress As Boolean)
            Me.oldValue = ScheduleWork.scheduleInfo
            ScheduleWork.scheduleInfo = New ScheduleInfo(executor, suppress)
        End Sub

        Public Overridable Sub Dispose()
            If (ScheduleWork.scheduleInfo.scheduleWork AndAlso Not ScheduleWork.scheduleInfo.suppress) Then
                ScheduleWork.scheduleInfo.executor.RequestHostingService
            End If
            ScheduleWork.scheduleInfo = Me.oldValue
        End Sub


        ' Properties
        Public Shared WriteOnly Property Executor As WorkflowExecutor
            Set(ByVal value As WorkflowExecutor)
                ScheduleWork.scheduleInfo.executor = value
            End Set
        End Property

        Public Shared WriteOnly Property NeedsService As Boolean
            Set(ByVal value As Boolean)
                ScheduleWork.scheduleInfo.scheduleWork = value
            End Set
        End Property


        ' Fields
        Protected oldValue As ScheduleInfo
        <ThreadStatic> _
        Protected Shared scheduleInfo As ScheduleInfo

        ' Nested Types
        Friend Class ScheduleInfo
            ' Methods
            Public Sub New(ByVal executor As WorkflowExecutor, ByVal suppress As Boolean)
                Me.suppress = suppress
                Me.scheduleWork = False
                Me.executor = executor
            End Sub


            ' Fields
            Public executor As WorkflowExecutor
            Public scheduleWork As Boolean
            Public suppress As Boolean
        End Class
    End Class
End Namespace

